package com.huishu.commons.utlis;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Slf4j
public class SendSmsUtils {

    private final static String username = "610755";

    private final static String password = "7550512";

    private final static String url = "http://61.129.57.37:7891/mt";

    public static boolean sendMessage(String phone, String message) {
        StringBuilder sb = new StringBuilder();
        // 短信相关的必须参数
        int transferEncoding = 3;
        int responseFormat = 2;
        int registeredDelivery = 1;
        sb.append(url);
        sb.append("?dc=8");// 表明发送的是中文
        try {
            sb.append("&sm=").append(URLEncoder.encode(message, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        sb.append("&da=").append(phone);
        sb.append("&un=").append(username);
        sb.append("&pw=").append(password);
        sb.append("&tf=").append(transferEncoding); // 表示短信内容为 urlencode+utf8
        sb.append("&rd=").append(registeredDelivery);//是否需要状态报告
        sb.append("&rf=").append(responseFormat);

        try {
            JSONObject object = HttpUtils.doGet(sb.toString(), "");
            log.info("发送短信 result:{}", object.toJSONString());
            return object.getBoolean("success");
        } catch (Exception e) {
            log.info("发送短信异常：{}", e.getMessage());
            return false;
        }
    }

    public static void main(String[] args) {
        String message = "【工派派】验证码：您收到一条用户报名信息，请登录工派派APP查看！下载链接：https://sourl.cn/tCW89d";
        System.out.println();
        sendMessage("15074150969", message);
    }

}


